升級指南

0.13.x-> 0.14.0

TypeScript定義

TypeScript軸定義已更新,以匹配API軸並使用ES2015模塊語法。

請使用以下import語句在TypeScript中導入軸:

import axios from 'axios';

axios.get('/foo')
  .then(response => console.log(response))
  .catch(error => console.log(error));

agent 配置選項

agent配置選項已被替換為兩個新的選項:httpAgenthttpsAgent請改用它們。

{
  // Define a custom agent for HTTP
  httpAgent: new http.Agent({ keepAlive: true }),
  // Define a custom agent for HTTPS
  httpsAgent: new https.Agent({ keepAlive: true })
}

progress 配置選項

progress配置選項已被替換的onUploadProgressonDownloadProgress選項。

{
  // Define a handler for upload progress events
  onUploadProgress: function (progressEvent) {
    // ...
  },

  // Define a handler for download progress events
  onDownloadProgress: function (progressEvent) {
    // ...
  }
}

0.12.x-> 0.13.0

0.13.0版本包含對自定義適配器和錯誤處理的一些更改。

錯誤處理

在此版本之前,錯誤可能是服務器響應中包含錯誤的狀態代碼或實際錯誤Error在此版本中,Promise始終會拒絕帶有Error如果收到響應,Error則還將包括響應。

axios.get('/user/12345')
  .catch((error) => {
    console.log(error.message);
    console.log(error.code); // Not always specified
    console.log(error.config); // The config that was used to make the request
    console.log(error.response); // Only available if response was received from the server
  });

要求適配器

  1. 現在,將響應轉換器稱為適配器的外部。
  2. 請求適配器返回Promise

這意味著您不再需要調用transformData響應數據。您也不再接收resolvereject在適配器中的參數。

先前的代碼:

function myAdapter(resolve, reject, config) {
  var response = {
    data: transformData(
      responseData,
      responseHeaders,
      config.transformResponse
    ),
    status: request.status,
    statusText: request.statusText,
    headers: responseHeaders
  };
  settle(resolve, reject, response);
}
function myAdapter(config) {
  return new Promise(function (resolve, reject) {
    var response = {
      data: responseData,
      status: request.status,
      statusText: request.statusText,
      headers: responseHeaders
    };
    settle(resolve, reject, response);
  });
}

有關更多詳細信息,請參見相關的提交:

0.5.x-> 0.6.0

0.6.0版本主要包含一些錯誤修復,但是升級時需要注意幾件事。

ES6 Promise Polyfill

Up until the 0.6.0 release ES6 Promise was being polyfilled using es6-promise. With this release, the polyfill has been removed, and you will need to supply it yourself if your environment needs it.

require('es6-promise').polyfill();
var axios = require('axios');

This will polyfill the global environment, and only needs to be done once.

axios.success/axios.error

The success, and error aliases were deprectated in 0.4.0. As of this release they have been removed entirely. Instead please use axios.then, and axios.catch respectively.

axios.get('some/url')
  .then(function (res) {
    /* ... */
  })
  .catch(function (err) {
    /* ... */
  });

UMD

Previous versions of axios shipped with an AMD, CommonJS, and Global build. This has all been rolled into a single UMD build.

// AMD
require(['bower_components/axios/dist/axios'], function (axios) {
  /* ... */
});

// CommonJS
var axios = require('axios/dist/axios');